package com.luocj.elephant.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.luocj.elephant.demo.R;
import com.luocj.elephant.demo.utils.Utils;

public class MyRingView extends View {

    private Paint mPaint;
    //中心位置
    private int center;
    //半径
    private int radius;
    //圆环宽度
    private int ringWidth;
    private int width;
    private int height;
    //    默认宽高
    private int result;
    private int ringColor;

    //进度
    private int progress;

    public int getProgress() {
        return progress;
    }



    public MyRingView(Context context) {
        this(context, null);
    }

    public MyRingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyRingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        result = Utils.dp2px(context, 100);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyRingView);
        ringColor = ta.getColor(R.styleable.MyRingView_myRingColor, Color.RED);
        ringWidth = (int) ta.getDimension(R.styleable.MyRingView_myRingWidth, Utils.dp2px(context, 20));

        ta.recycle();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高 模式 高度
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = result;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = result;
        }

        int min = Math.min(width, height);
        setMeasuredDimension(min, min);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        center = getWidth() / 2;
        radius = (center - ringWidth / 2);
        drawCicle(canvas);
//        中心文字
        drawContentText(canvas);
        drawProgress(canvas);
    }

    private void drawProgress(Canvas canvas) {
        mPaint.setStrokeWidth(ringWidth);
        mPaint.setColor(Color.BLACK);
        //Stroke样式
        RectF strokeOval = new RectF(center - radius,
                center - radius,
                center + radius,
                center + radius);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        canvas.drawArc(strokeOval, -90, 360 * progress / 100, false, mPaint);

    }

    private void drawContentText(Canvas canvas) {
        mPaint.setStrokeWidth(0);
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(Utils.dp2px(getContext(), 16));
        mPaint.setTypeface(Typeface.DEFAULT);
        String text = "100%";
        float v = mPaint.measureText(text);
        canvas.drawText(text, center - v / 2, center, mPaint);

    }

    private void drawCicle(Canvas canvas) {
        mPaint.setColor(ringColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(ringWidth);
        canvas.drawCircle(center, center, radius, mPaint);
    }
    public void setProgress(int progress) {

        if (progress <0){
            throw  new RuntimeException("shit progress should > 0");
        }

        if (progress >100){
            throw new RuntimeException("progress should < 100");
        }

        this.progress = progress;
        postInvalidate();
    }

}
